package com.baosight.sczx.common.zm.cInterface.cp;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanCM;

public class ProducePlanCM {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 验证是否存在记录
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean validate(Map<String, String> params) {
		if (params == null) {
			return true;
		}
		
		List result = dao.query("ZMCPProducePlanCM.validate", params);
		if(result != null && result.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 从采煤计划明细表中
	 * 获得专业采煤计划在上报的时候的统计值
	 * 原煤产量、保护层面积、厚保面积比、原煤灰份、推进、工效
	 * 原煤产量 = 容重 * 采长 * 采高 * 推进，暂不计算，直接填写
	 * @param fdProfessionalplanId
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, BigDecimal> CMModify(String fdProfessionalplanId, String fdProduceplanId) {
		if(fdProfessionalplanId == null && fdProduceplanId == null) {
			return null;
		}
		
		Map<String, String> _params = new HashMap<String, String>();
		_params.put("fdProfessionalplanId", fdProfessionalplanId);
		_params.put("fdProduceplanId", fdProduceplanId);
				
		List result = dao.query("ZMCPProducePlanCM.query", _params);
		
		Map<String, BigDecimal> _map = new HashMap<String, BigDecimal>();
				
		Double fdPlanMine = new Double(0); //原煤产量
		Double fdZhcmj = new Double(0);//中厚层面积
		Double fdBhcmj = new Double(0);//保护层面积
		Double _fdAshIndex = new Double(0); //灰分*原煤量
		Double fdAshIndex = new Double(0); //灰分
		Double fdBasedIn = new Double(0);//推进度
		Double fdRecoveryRate = new Double(0);//功效
		
		for (int i = 0; i < result.size(); i++) {
			ZMCPProducePlanCM plan = (ZMCPProducePlanCM)result.get(i);
			fdPlanMine += plan.getFdPlanSum() == null ? 0 : plan.getFdPlanSum().doubleValue();//采煤计划量
			fdZhcmj += plan.getFdZhcmj() == null ? 0 : plan.getFdZhcmj().doubleValue();//中厚层面积
			fdBhcmj += plan.getFdBhcmj() == null ? 0 : plan.getFdBhcmj().doubleValue();//保护层面积
			_fdAshIndex += plan.getFdAshIndex() == null ? 0 : plan.getFdAshIndex().doubleValue() * plan.getFdPlanSum().doubleValue();//灰分*原煤量
			fdBasedIn += plan.getFdBasedIn() == null ? 0 : plan.getFdBasedIn().doubleValue();//推进
			fdRecoveryRate += plan.getFdRecoveryRate() == null ? 0 : plan.getFdRecoveryRate().doubleValue();//功效，<字段，回采率当功效>
		}
				
		if( fdPlanMine != 0 && _fdAshIndex != 0) {
			fdAshIndex = _fdAshIndex / fdPlanMine;
		}
		
		if (result != null && result.size() > 0) {
			fdRecoveryRate = fdRecoveryRate / result.size();
		}
		
		_map.put("ymSum", BigDecimal.valueOf(fdPlanMine));		//原煤产量
		_map.put("hf", BigDecimal.valueOf(fdAshIndex));			//原煤灰分
		_map.put("bhcmj", BigDecimal.valueOf(fdBhcmj));			//保护层面积
		_map.put("zhmcmj", BigDecimal.valueOf(fdZhcmj));		//中厚煤层面积
		_map.put("tjd", BigDecimal.valueOf(fdBasedIn));			//推进
		_map.put("gx", BigDecimal.valueOf(fdRecoveryRate));		//功效

		return _map;
	}
	
	/**
	 * 专业计划上报成功
	 * 修改采煤明细的 生产计划字段
	 * 复制计划
	 */
	@SuppressWarnings("rawtypes")
	public static void cloneCM(String fdProduceplanId, String fdProfessionalplanId) {
		if(fdProduceplanId != null && !fdProduceplanId.trim().equals("") && fdProfessionalplanId != null && !fdProfessionalplanId.trim().equals("")) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			
			List result = dao.query("ZMCPProducePlanCM.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanCM cm = (ZMCPProducePlanCM)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_CM");
				cm.setFdProfessionalplanId("");
				cm.setFdKeyId(fdKeyId);
				cm.setFdProduceplanId(fdProduceplanId);
				
				dao.insert("ZMCPProducePlanCM.insert", cm.toMap());
			}
		}
	}
	
	/**
	 * 生产计划调整时，复制采煤计划
	 * @param fdProduceplanId
	 */
	@SuppressWarnings("rawtypes")
	public static void cloneByAdjust(String oldId, String newId) {
		if(oldId != null && newId != null) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProduceplanId", oldId);
			
			List result = dao.query("ZMCPProducePlanCM.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanCM cm = (ZMCPProducePlanCM)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_CM");
				cm.setFdKeyId(fdKeyId);
				cm.setFdProduceplanId(newId);
				
				dao.insert("ZMCPProducePlanCM.insert", cm.toMap());
			}
		}
	}
}
